package com.qf.web.servlet;

import com.qf.domain.Goods;
import com.qf.domain.PageBean;
import com.qf.sevice.GoodsService;
import com.qf.sevice.Impl.GoodsServiceImpl;
import com.qf.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * projectName:Shopping
 * author:RongChao
 * time:2022/4/19 17:49
 * description:
 */
@WebServlet("/goodsservlet")
public class GoodsServlet extends BaseServlet {
    GoodsService goodsService = new GoodsServiceImpl();

    public String getGoodsById(HttpServletRequest request, HttpServletResponse response) throws Exception {
        int id = Integer.parseInt(request.getParameter("id"));
        Goods goods = goodsService.findById(id);
        request.setAttribute("goods",goods);
        return "forward:/goodsDetail.jsp";

    }


    public String getGoodsListByTypeId(HttpServletRequest request, HttpServletResponse response) throws Exception{
        int typeId = Integer.parseInt(request.getParameter("typeId"));
        String pageNumStr = request.getParameter("pageNum");
        String pageSizeStr = request.getParameter("pageSize");
        int pageNum = 1;
        int pageSize = 4;
        if (!StringUtils.isEmpty(pageNumStr)){
            pageNum = Integer.getInteger(pageNumStr);
        }
        if (!StringUtils.isEmpty(pageSizeStr)){
            pageSize = Integer.getInteger(pageSizeStr);
        }
        PageBean pageBean = goodsService.findByPage(typeId,pageNum,pageSize);
        request.setAttribute("pageBean",pageBean);
        request.setAttribute("typeId",typeId);
        return "forward:/goodsList.jsp";

    }
}
